package com.sojson.config.exception;

import java.io.IOException;
import java.sql.SQLException;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sojson.config.exception.impl.GlobalExceptionHandlerOnlineImpl;
import com.sojson.config.exception.impl.GlobalExceptionHandlerTestImpl;
import com.sojson.constant.ConstantByMap;
import com.sojson.enums.core.EEnvironmentType;
import com.sojson.result.Result;

/**
 * 全局异常类
 * 
 * @author liu
 * @date 2021-09-07
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    private GlobalExceptionHandlerInterface globalExceptionHandlerInterface = getGlobalExceptionHandler();

    /**
     * 返回错误信息
     * 
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    private Object defaultErrorHandlerToString(Exception e) {
        TransException transException = null;
        globalExceptionHandlerInterface.defaultErrorHandler(e);
        if (e instanceof TransException) {
            // 处理TransException
            transException = (TransException)e;
            return response(transException);
        }
        if (e instanceof TransNoException) {
            // 此异常不做处理
            return null;
        }

        transException = new TransException();
        transException.setErrCode(TransErrorCode.FAIL.getCode());
        transException.setErrMsg(getErrorMessage(e));
        return response(transException);
    }

    /**
     * 返回错误信息
     * 
     * @param err
     * @return
     */
    private Object response(TransException err) {
        return Result.fail(err);
    }

    /**
     * 获取错误信息
     * 
     * @param ex
     * @return
     */
    private String getErrorMessage(Exception ex) {
        String result = "";
        if (ex instanceof SQLException) {
            result = "数据库操作错误:" + ex.getMessage();
        } else if (ex instanceof NullPointerException) {
            result = "空指针异常:" + ex.getMessage();
        } else if (ex instanceof NoSuchMethodException) {
            result = "方法名错误:" + ex.getMessage();
        } else if (ex instanceof IOException) {
            result = "输入输出流错误:" + ex.getMessage();
        } else if (ex instanceof ClassCastException) {
            result = "类型强制转化错误:" + ex.getMessage();
        } else if (ex instanceof Exception) {
            // result = "程序内部发生错误:" + ex.getMessage();
            result = ex.getMessage();
        }
        return result;
    }

    /**
     * 获取异常统一处理实现类
     * 
     * @return
     */
    private GlobalExceptionHandlerInterface getGlobalExceptionHandler() {
        if (EEnvironmentType.TEST.getCode() == ConstantByMap.ENVIRONMENT) {
            return GlobalExceptionHandlerTestImpl.getInstances();
        } else if (EEnvironmentType.ONLINE.getCode() == ConstantByMap.ENVIRONMENT) {
            return GlobalExceptionHandlerOnlineImpl.getInstances();
        }
        return GlobalExceptionHandlerTestImpl.getInstances();
    }

    // /**
    // * 用来处理bean validation异常
    // * @param ex
    // * @return
    // */
    // @ExceptionHandler(BindException.class)
    // @ResponseBody
    // public Result<?> resolveConstraintViolationException(BindException ex) {
    // List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
    //
    // if (!CollectionUtils.isEmpty(objectErrors)) {
    // StringBuilder msgBuilder = new StringBuilder();
    // for (ObjectError objectError : objectErrors) {
    // msgBuilder.append(objectError.getDefaultMessage()).append(",");
    // }
    // String errorMessage = msgBuilder.toString();
    // if (errorMessage.length() > 1) {
    // errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
    // }
    // return Result.fail(errorMessage);
    // }
    //
    // return Result.fail(ex.getMessage());
    // }
    //
    // @ExceptionHandler(MethodArgumentNotValidException.class)
    // @ResponseBody
    // public Result<?> resolveMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
    // List<ObjectError> objectErrors = ex.getBindingResult().getAllErrors();
    // if (!CollectionUtils.isEmpty(objectErrors)) {
    // StringBuilder msgBuilder = new StringBuilder();
    // for (ObjectError objectError : objectErrors) {
    // msgBuilder.append(objectError.getDefaultMessage()).append(",");
    // }
    // String errorMessage = msgBuilder.toString();
    // if (errorMessage.length() > 1) {
    // errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
    // }
    // return Result.fail(errorMessage);
    // }
    //
    // return Result.fail(ex.getMessage());
    // }
    //
    // @ExceptionHandler(TransException.class)
    // @ResponseBody
    // public Result<?> customException(TransException ex) {
    // return Result.fail(ex.getErrMsg());
    // }

}
